package com.example.zbh.dsliao.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.example.zbh.dsliao.R;
import com.example.zbh.dsliao.db.entity.ForYouRecommend;
import com.example.zbh.dsliao.ui.activty.SongListActivity;
import com.example.zbh.dsliao.utils.net.VolleySingleton;

import java.util.List;

/**
 * Created by yaolin on 15/11/20.
 */
public class RVForYouRecommendAdapter extends RecyclerView.Adapter<RVForYouRecommendAdapter.MyViewHolder> {
    private List<ForYouRecommend>  datas;
    private Context context;

    private ImageLoader imageLoader;


    public void addData(List<ForYouRecommend> datas ){
        this.datas = datas;
        notifyDataSetChanged();
    }


    public RVForYouRecommendAdapter(Context context) {
        this.context = context;
        imageLoader = VolleySingleton.getInstance().getImageLoader();
    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View v = inflater.inflate(R.layout.list_for_you_recommend, null);
        return new MyViewHolder(v);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        if (datas != null && datas.size() > 0){
            ForYouRecommend f = datas.get(position);
            if(f.getPic_big() != null){
                String imgUrl = f.getPic_big();
                getBitmap(imgUrl, holder.img);
            }
            holder.position = position;
        }

    }

    private void getBitmap(String url, NetworkImageView imageView) {
        imageView.setDefaultImageResId(R.mipmap.music_cache_icon);
        imageView.setErrorImageResId(R.mipmap.music_cache_icon);
        imageView.setImageUrl(url, imageLoader);
    }

    @Override
    public int getItemCount() {
        return datas != null && datas.size() > 0 ? 4 : 0;
    }

    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private NetworkImageView img;
        private int position;

        public MyViewHolder(View itemView) {
            super(itemView);
            img = (NetworkImageView) itemView.findViewById(R.id.img);
            img.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {

            if(datas != null && datas.size() > 0){
                ForYouRecommend f = datas.get(position);
                Intent intent = new Intent(context, SongListActivity.class);
                context.startActivity(intent);
            }
        }
    }
}
